﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/*
 * CameraControl
 * 控制镜头
 *
 *
 * 依赖：
 *
 * ！不写重复代码！
 * @author TTun
 * @date 7/14/2017 11:31:51 AM
 */
namespace TTGameEngine {

public class CameraControl : MonoBehaviour {
    
    [SerializeField,ReadOnly]
    Transform m_lookAtTargetTF;
    public Vector3 m_offsetPos = new Vector3(0,0,-10f);
    public Camera m_camera;

    public Transform tf;

    //切关Loading的时候隐藏Camera。Awake时就隐藏，一帧都不放过。
    public static bool IsLoading = false;


    public Transform LookAtTargetTF {
        get {
            return m_lookAtTargetTF;

        }
//         set {
//             if(value != m_lookAtTargetTF) {
//                 m_lookAtTargetTF = value;
//                 lookAtTarget(m_lookAtTargetTF, m_offsetPos);
//             }
//         }
    }

    void Awake() {
        tf = transform;
        if(m_camera == null) m_camera = GetComponent<Camera>();
        if(m_camera != null) {
            if(IsLoading) {
                m_camera.enabled = false;
            }
        }
    }

    public void LoadingOKStartRender() {
        if(m_camera != null) {
            m_camera.enabled = true;
        }
        IsLoading = false;
    }

    // Use this for initialization
    void Start() {

    }

    // Update is called once per frame
    void Update() {
        if(LookAtTargetTF != null) {
            lookAtTarget(m_lookAtTargetTF, m_offsetPos);
        }
    }

    public void LockTarget(Transform aTargetTF) {
        m_lookAtTargetTF = aTargetTF;
    }

    void lookAtTarget(Transform aTargetTF, Vector3 aOffsetPos) {
        tf.position = aTargetTF.position + aOffsetPos;
        tf.LookAt(aTargetTF);
    }
}

}
